function spawn_stairs(player, grid)
	-- should be called right after player spawns

	Stair = { x = 0, y = 0, name = "stairs_up", tt = "stairs", image = env_sheet_quads[4],
		explored = true}

	Stair.x = player.x
	Stair.y = player.y

	table.insert(local_map.features, Stair)

	-- stairs down here

	-- Stair = { x = 0, y = 0, name = "stairs_down", tt = "stairs", image = env_sheet_quads[1] }

	spawn_stairs_down(player, grid)

end

function spawn_stairs_down(player, grid)
	-- should be called during spawn_stairs
	local stair_upX = player.x
	local stair_upY = player.y

	local potX = 0
	local potY = 0

	local dX = 0
	local dY = 0

	local min_dist = 20

	local placed = false

	while placed == false do

		potX = RNG:random(grid_size)
		potY = RNG:random(grid_size)
		dX, dY = map_distance(stair_upX, stair_upY, potX, potY)

		if dX > 15 and dY > 15 then

			if grid[potX][potY].type == "floor" then

				placed = true

			end
		end
	end



	Stair = { x = potX*tile_size, y = potY*tile_size,
		name = "stairs_down", tt = "stairs", image = env_sheet_quads[1],
		explored = false
	}

	table.insert(local_map.features, Stair)


end

function draw_features()

	for i = 1, #data.player.local_map.features do

		if data.player.local_map.features[i] ~= nil then
			if data.player.local_map.features[i].tt == "stairs" then
				draw_in_fov(data.player.local_map.features[i])
			elseif data.player.local_map.features[i].tt == "nerd" then
				if data.player.local_map.features[i].visible == true then
					draw_in_fov(data.player.local_map.features[i])
				end
			elseif data.player.local_map.features[i].tt == "computer" then
				draw_in_fov(data.player.local_map.features[i])
			else
				draw_in_fov(data.player.local_map.features[i])
			end
		end

	end


end


function features_ai(features)

	local nerd_tar_x
	local nerd_tar_y

	--print(table.tostring(features))

	for i = 1, #features do
		if features[i].turn ~= nil then

			features[i].turn = true

		end

	end


	for i = 1, #features do


		if features[i].tt == "nerd" then
			if features[i].turn == true then
				print("nerd with turn found at ",
					features[i].x, " ", features[i].y)

				nerd_tar = nerd_pov(features[i])
				--print(table.tostring(nerd_tar))
				if nerd_tar ~= nil then
					nerd_attack(features[i], nerd_tar)
				end
				features[i].turn = false

			end

		end

	end


end

function nerd_attack(nerd, nerd_tar)

	if nerd_tar ~= nil and nerd ~= nil
		and nerd_tar.x ~= nil and nerd_tar.y ~= nil then
		--print(table.tostring(nerd_tar))
		if p_fov(nerd_tar.x, nerd_tar.y) then

			local ns = append_strings("Nerd attacks ", nerd_tar.name)
			printc(ns)

		end
		print("nerd damages ", nerd_tar.name, " for ", nerd.damage)
		nerd_tar.health = nerd_tar.health - nerd.damage
	end



end

function nerd_pov(nerd)

	local nerd_enemies = {}
	local nerd_tar_x = 0
	local nerd_tar_y = 0
	local targetted = false
	local target = {}

	if p_fov(nerd.x, nerd.y) then

		nerd.visible = true
		target = data.player

	else
		nerd.visible = false
		for i = 1, #local_map.enemies do

			if local_map.enemies[i] ~= nil
				and local_map.enemies[i].x ~= nil
				and local_map.enemies[i].y ~= nil then

				if local_map.enemies[i].x < nerd.x+(nerd.fov*tile_size)
					and local_map.enemies[i].x > nerd.x-(nerd.fov*tile_size)
					and local_map.enemies[i].y < nerd.y+(nerd.fov*tile_size)
					and local_map.enemies[i].y > nerd.y-(nerd.fov*tile_size) then

					table.insert(nerd_enemies, local_map.enemies[i])

					if targetted == false then
						print("nerd targets ", local_map.enemies[i].name)

						nerd_tar_x = local_map.enemies[i].x
						nerd_tar_y = local_map.enemies[i].y
						target = local_map.enemies[i]
						targetted = true

					end



				end

			end

		end

		if targetted == false then

			move_nerd(nerd)

		end

	end

	--print(table.tostring(target))

	return target


end

function move_nerd(nerd)

	local r = RNG:random(-4, 4)
	local r2 = RNG:random(-4, 4)


	r = r*tile_size
	r2 = r2*tile_size

	if nerd.x+r < grid_size*tile_size-tile_size
		and nerd.y+r2 < grid_size*tile_size-tile_size
		and nerd.x+r > 0 and nerd.y+r2 > 0 then

		if feature_check(nerd.x+r, nerd.y+r2) then

		else
			nerd.x = nerd.x+r
			nerd.y = nerd.y+r
		end

	end


	if nerd.x > grid_size*tile_size
		or nerd.y > grid_size*tile_size
		or nerd.x < 0 or nerd.y < 0 then

		nerd.x = (grid_size/2)*tile_size
		nerd.y = (grid_size/2)*tile_size

	end




end

function spawn_computer(tiles)

	local rX = RNG:random(grid_size)
	local rY = RNG:random(grid_size)

	for i = 1, #tiles do

		if tiles[i].x == rX*tile_size
			and tiles[i].y == rY*tile_size then

			if tiles[i].type == "floor" then
				Computer = { image = computer_img, x = rX*tile_size,
					y = rY*tile_size, explored = false, id = nerd_counter,
					name = "computer", tt = "computer"}

				table.insert(local_map.features, Computer)

			end

		end

	end

end

function feature_check(x,y, features)

	if features ~= nil then
		for i = 1, #features do
			if features[i].x == x and features[i].y == y then
				return true
			end

		end
	end

	return false

end

function spawn_crater(x, y)

	Crater = { image = crater_img, x = x, y = y, explored = false }

	table.insert(local_map.features, Crater)


end


function spawn_nerd(tiles)

	local r = 0
	local nerd_placed = false
	local diffMod = data.player.current_harvest*4
	nerd_counter = nerd_counter+1
	local nR = RNG:random(1000)

	if nR > 950 --[[and data.player.current_harvest > 1]] then

		for i = 1, #tiles do

			if tiles[i] ~= nil then

				if tiles[i].type == "floor" then

					r = RNG:random(500)
					if r > 498 and nerd_placed == false then
						print("nerd spawned")

						Nerd = { x = tiles[i].x, y = tiles[i].y,
							name = "nerd", tt = "nerd", image = nerd_img,
							explored = false, health = RNG:random(40, 100)+diffMod,
							damage = RNG:random(30, 40)+data.player.current_harvest,
							xp = 0, visible = false, seen = false,
							explored = false, turn = false, fov = 4, inventory = {},
							id = nerd_counter
						}

						Nerd.xp = Nerd.damage + Nerd.health + diffMod

						table.insert(local_map.features, Nerd)
						nerd_placed = true

					end


				end

			end

		end

		spawn_computer(tiles)
	end

end
